package cn.zr.aitool.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import java.util.List;

/**
 * Created by zhengrui on 2015/8/28.
 */
public class DbUtils {
    public final static int TPYE_AND = 1;
    public final static int TPYE_OR = 2;

    /**
     * @param selectionRow =null 查询所有 格式： String[]{row1,row2}
     * @param selectionArgs 格式： String[]{arg1,arg2}
     * @param type          搜索条件间关系 1：and，2 or
     * @return
     */
    public static Cursor queryByTwoSelecionBase(SQLiteDatabase db, String tableName, String[] selectionRow, String[] selectionArgs, String groupby, String having, String order, String limit, int type) {
        String relation;
        String selection=null;

        if (type == TPYE_AND) {
            relation = " AND ";
        } else {
            relation = " OR ";
        }

        if(selectionRow!=null&&selectionRow.length>0){
            StringBuilder tempSelection = new StringBuilder();
            tempSelection.append(selectionRow[0]).append("=? ");
            if (selectionRow.length > 1) {
                tempSelection.append(relation).append(selectionRow[1]).append("=? ");
            }
            selection=tempSelection.toString();
        }
        return db.query(tableName, null, selection, selectionArgs, groupby, having, order,limit);
    }

    public static void closeCursor(Cursor cursor) {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }

    public static long insert(SQLiteDatabase db, String table, List<ContentValues> values) {
        db.beginTransaction();
        long rid=0;
        try {
            for (int i = 0; i < values.size(); i++) {
                rid = db.insertWithOnConflict(table, null, values.get(i), SQLiteDatabase.CONFLICT_IGNORE);
            }
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
        }
        return rid;
    }

    /**
     * @param table          表名
     * @param countRow       需要计数的列
     * @param selection2     条件语句
     * @param selectionArgs2 参数
     * @return
     */
    public static int queryCountBySelection(SQLiteDatabase db, String table, String countRow, String selection2, String[] selectionArgs2) {
        int count = 0;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("select count(" + countRow + ") from " + table + " where " + selection2, selectionArgs2);
            if (null != cursor && cursor.moveToFirst()) {
                count = cursor.getInt(0);
            }

        } catch (Exception e) {
            e.toString();
        } finally {
            closeCursor(cursor);
        }
        return count;
    }

    public static String checkValue(String checkValue){
        if (null == checkValue) {
            checkValue = "";
        }
        return checkValue;
    }


}
